﻿
#include<libpq-fe.h>
#define MAXPARAMNUM 20
#define ERRORMESSAGE 100
#define SQL_NUM 1024
//数据库实体-----------------------------------
typedef struct D_TimeStamp D_TimeStamp;
typedef struct D_User D_User;
typedef struct D_configure D_configure;
typedef struct D_third_login D_third_login;
typedef struct D_im_sms D_im_sms;
typedef struct D_im_user_email D_im_user_email;
typedef struct D_im_login_record D_im_login_record;
typedef struct D_im_relation_applicant D_im_relation_applicant;
typedef struct D_im_user_relation D_im_user_relation;

typedef struct DB_Command_Prepared DB_Command_Prepared;
typedef struct DB_Command_Value DB_Command_Value;
typedef struct DB_Rsult_Data DB_Rsult_Data;
typedef struct DB_Result_func_Array DB_Result_func_Array;


typedef struct IMConfigureSave IMConfigureSave;

typedef int(*Result_Func)(PGresult *result, DB_Rsult_Data *result_data);

/*
db_mapper.c

*/
extern DB_Command_Prepared db_query_imUser_by_id;
extern DB_Command_Prepared db_insert_imUser;
extern DB_Command_Prepared db_query_imUser_by_tel;
extern DB_Command_Prepared db_query_imUser_by_user_name;
extern DB_Command_Prepared db_delete_imUser;
extern DB_Command_Prepared db_update_imUser;
extern DB_Command_Prepared db_query_imUserFriend_by_id;

extern DB_Command_Prepared db_query_imConfigure_by_configur_name;

extern DB_Command_Prepared db_query_im_third_login_by_openid_and_thirdid;
extern DB_Command_Prepared db_query_im_third_login_by_userid_and_thirdid;
extern DB_Command_Prepared db_query_im_third_login_by_userid;
extern DB_Command_Prepared db_insert_im_third_login;
extern DB_Command_Prepared db_delete_im_third_login_by_userIdAndThirdId;

extern DB_Command_Prepared db_insert_im_sms;
extern DB_Command_Prepared db_query_im_sms_by_type_and_contactvalue;
extern DB_Command_Prepared db_updata_im_sms_by_identity_no;


extern DB_Command_Prepared db_insert_im_user_email;
extern DB_Command_Prepared db_update_im_user_email;
extern DB_Command_Prepared db_query_im_user_email_by_email;
extern DB_Command_Prepared db_query_im_user_email_by_id;
extern DB_Command_Prepared db_query_im_user_email_by_idAndEmail;
extern DB_Command_Prepared db_query_im_user_email_by_emailAndPermition;
extern DB_Command_Prepared db_query_im_user_email_by_idAndPermition;
extern DB_Command_Prepared db_delete_im_user_email_by_idAndEmail;

extern DB_Command_Prepared db_insert_im_login_record;

extern DB_Command_Prepared db_insert_im_relation_applicant;
extern DB_Command_Prepared db_update_im_relation_applicant;
extern DB_Command_Prepared db_query_im_relation_applicant_by_respondentId;
extern DB_Command_Prepared db_query_im_relation_applicant_by_applicantId;
extern DB_Command_Prepared db_query_im_relation_applicant_by_relationApplicantId;

extern DB_Command_Prepared db_insert_im_user_relation;
extern DB_Command_Prepared db_update_im_user_relation;
extern DB_Command_Prepared db_query_im_user_relation_by_UserIdAndFriendId;
extern DB_Command_Prepared db_query_im_user_relation_by_UserId;

/*
db_crud.c
*/
//对外提供的数据库查询函数
D_User * queryImUserById(int id, int *datalen);
D_User * queryImUserByUserName(char *user_name, int *datalen);
D_User * insertImUser(D_User *user, int *status, char *error_message);
D_User *deleteImUserById(int id,int *status, char *error_message);
D_User *updataImUser(D_User *user,int *status, char *error_message);
D_User * queryImUserByTel(char *tel, int *datalen);
D_User * queryImUserByEmail(char *email, int *datalen);
D_User * queryImUserFriendsByUserId(int userid, int *datalen);

D_configure * queryImConfigureByName(char *configure_name, int *datalen);
char * queryImConfigureValue(char *configure_name);

D_third_login *queryImThirdLoginByOpenidAndThirdid(char *openid,int thirdId,int *datalen);
D_third_login *queryImThirdLoginByUseridAndThirdid(int userid,int thirdId,int *datalen);
D_third_login *queryImThirdLoginByUserid(int userid,int *datalen);
D_third_login *insertImThirdLogin(D_third_login *third_data, int *status, char *error_message);
D_third_login *deleteImThirdLoginByUseridAndThirdId(int userId,int thirdId,int *datalen);

D_im_sms * insertImSms(D_im_sms *sms_data, int *status, char *error_message);
D_im_sms * queryImSMSByValueAndTypeAndTime(char *contactvalue,int sms_type,int channel,D_TimeStamp* beginTime,D_TimeStamp* endTime, int *datalen);
D_im_sms * queryImSMSByValueAndType(char *contactvalue,int sms_type,int channel,int min,int *datalen);
D_im_sms * updataImSMS(D_im_sms *sms_data,int *status, char *error_message);

D_im_user_email * updateImUserEmail(D_im_user_email* userEmail,int *status, char *error_message);
D_im_user_email * insertImUserEmail(D_im_user_email* userEmail,int *status, char *error_message);
D_im_user_email * queryImUserEmailByEmail(char *email, int *datalen);
D_im_user_email * queryImUserEmailById(int id, int *datalen);
D_im_user_email * queryImUserEmailByEmailAndPermition(char *email,int permition, int *datalen);
D_im_user_email * queryImUserEmailByIdAndPermition(int id,int permition, int *datalen);
D_im_user_email * queryImUserEmailByIdAndEmail(int id,char * email, int *datalen);
D_im_user_email * deleteImUserEmailByIdAndEmail(int id,char * email, int *datalen);

D_im_relation_applicant * InsertImRelationApplicant(D_im_relation_applicant* data, int *datalen);
D_im_relation_applicant * UpdataImRelationApplicant(D_im_relation_applicant* data, int *datalen);
D_im_relation_applicant * queryImRelationApplicantByRespondentId(int id, int *datalen);
D_im_relation_applicant * queryImRelationApplicantByApplicantId(int id, int *datalen);
D_im_relation_applicant * queryImRelationApplicantByRelationApplicantId(int id, int *datalen);

D_im_user_relation * InsertImUserRelation(D_im_user_relation* data, int *datalen);
D_im_user_relation * UpdataImUserRelation(D_im_user_relation* data, int *datalen);
D_im_user_relation * queryImUserRelationByUserIdAndFriendId(int userid,int friendid, int *datalen);
D_im_user_relation * queryImUserRelationByUserId(int userid, int *datalen);

D_im_login_record * insertImLoginRecord(D_im_login_record* data,int *status, char *error_message);
int userLoginLog(int userId,char* ip, char* mac);
/*
db_util.c
*/
//对外提供的数据库工具类函数
extern int db_string_to_timestamp(D_TimeStamp *timestamp, char *stime);
extern int db_timestamp_to_string(const D_TimeStamp *timestamp, char *stime);
extern char * db_int_to_string(int num);
extern void D_getCurrectTime(D_TimeStamp *p);
void D_getTimeByTime_t(D_TimeStamp *p,time_t *timep);
IMConfigureSave* D_FindImConfigureSaveNode(IMConfigureSave* head,char *configure_name);
int *PQresult_row_col_To_int(PGresult *result,int row,int col);
char *PQresult_row_col_To_char(PGresult *result,int row,int col);
D_TimeStamp *PQresult_row_col_To_D_TimeStamp(PGresult *result,int row,int col);
int D_SMSVerificationCode(char * contactvalue, char *code,SMS_MODE channal,SMS_STATUS status);

/*
db_conf.c
*/
extern int database_init();
extern int database_config_posttest(int *errs);
extern int database_config_test(ConfigFile *cf, ConfigEntry *ce, int type, int *errs);
extern int database_config_run(ConfigFile *cf, ConfigEntry *ce, int type);
extern int db_Command_to_Prepared(DB_Command_Prepared prepared, Result_Func func);
extern int db_databaseConnectStatus();
extern int db_queryByPerpared(DB_Command_Value command_value, DB_Rsult_Data *result_data);
extern int insert_byPerpared(DB_Command_Value command_value, DB_Rsult_Data *result_data);
extern int result_func_add(char* sql_name, int(*func)(PGresult *result, DB_Rsult_Data *result_data));
extern Result_Func result_func_select(char *sql_name);



/*
db_refunc,c
*/
extern int db_PQresult_To_D_User(PGresult *result, DB_Rsult_Data *result_data);
extern int db_PQresult_To_D_configure(PGresult *result, DB_Rsult_Data *result_data);
extern int db_PQresult_To_D_third_login(PGresult *result, DB_Rsult_Data *result_data);
extern int db_PQresult_To_D_im_sms(PGresult *result, DB_Rsult_Data *result_data);
extern int db_PQresult_To_D_im_user_email(PGresult *result, DB_Rsult_Data *result_data);
extern int db_PQresult_To_D_im_login_record(PGresult *result, DB_Rsult_Data *result_data);
extern int db_PQresult_To_D_im_relation_applicant(PGresult *result, DB_Rsult_Data *result_data);
extern int db_PQresult_To_D_im_user_relation(PGresult *result, DB_Rsult_Data *result_data);

extern D_User * db_createUser();
extern D_configure * db_createConfigure();
extern D_im_sms * db_createImSMS();
extern D_third_login * db_createThirdLogin();
extern D_im_user_email *db_createUserEmail();
extern D_im_login_record * db_createLoginRecord();
extern D_im_relation_applicant * db_createRelationApplicant();
extern D_im_user_relation * db_createUserRelation();

extern void db_D_UserFree(D_User *user, int len);
extern void db_D_ImSMSFree(D_im_sms* sms,int len);
extern void db_D_ThirdLoginFree(D_third_login * login,int len);
extern void db_D_configure_free(D_configure *d_configure,int len);
extern void db_D_UserEmailFree(D_im_user_email * user_email,int len);
extern void db_D_LoginRecordFree(D_im_login_record* data,int len);
extern void db_D_imRelationApplicantFree(D_im_relation_applicant* data,int len);
extern void db_D_imUserRelationFree(D_im_user_relation* data,int len);



//时间戳对应表格
struct D_TimeStamp {
	int year;
	int month;
	int day;
	int hour;
	int minute;
	int second;
};

//im_user表(用户信息表)
struct D_User {
	int *id;//用户在系统中唯一的标识
	int *git_id;//在gitee上面的id
	char *user_name;//用户名
	char *user_password;//用户密码
	char *tel;//手机号
	int *sex;//性别，女性为0，男性为1
	D_TimeStamp *created_at;//创建时间
	char *user_nick; //用户昵称
};

//im_configure表(配置信息表)
struct D_configure{
	char *configure_name;
	char *configure_value;
	char *configure_detail;
};

//im_third_login表(第三方登录的表)
struct D_third_login{
	int *third_id;
	char *open_id;
	int *user_id;
	char *nick_name;
};

//im_sms表，验证码表
struct D_im_sms{
	int *identity_no;				//验证码序号
	int *sms_type;					//验证码类型
	int *sms_channel;				//发送通道，例如1为手机号，2为邮箱等
	int *used;					//当前验证码是否已被使用0为未用，1为已用
	char *code;						//验证码
	char *contactvalue;					//用户联系方式，当sms_channel为1时为手机号，2为邮箱
	D_TimeStamp *valid_time;		//验证码有效时间
};

struct D_im_user_email{
	int *id;//用户在系统中唯一的标识
	char *email;//邮箱号码
	int *permition;
};

struct D_im_login_record{
	int* login_no;
	int* user_id;
	D_TimeStamp* log_time;
	char* login_ip;
	char* login_mac;
};

struct D_im_relation_applicant{
	int* relation_applicant_id;		//关系申请id
	int* applicant_type;			//申请情况，1为加好友申请，2为申请加组，3为邀请加组
	int* applicant_id;				//申请人id
	int* respondent_id;				//被申请人id，在申请加组时为空
	int* project_group_id;			//组序号,在好友申请时为空
	int* applicant_status;			//状态，1为未同意，2,为同意，3为拒绝
	D_TimeStamp * applicant_time; 	//申请时间
};

enum RelationApplicantType{
	ADDFRIEND = 1,
	ADDGRPOUP = 2,
	INVITEGROUP = 3,
};

enum RalationApplicantStatus{
	NOSELECT = 1,
	AGREE = 2,
	REFUSE = 3,
};

struct D_im_user_relation{
	int* user_id;					//用户在系统中唯一的标识
	int* friend_id;					//朋友用户在系统中唯一的标识
	char* nick_name;			//朋友备注名
};


//数据库配置结构体----------------------------------

//数据库预备命令语句结构体
struct DB_Command_Prepared {
	char *command;
	char *stmtName;
	int nParams;
	char paramType[MAXPARAMNUM][20];
	int isperared;//判断是否被预先加载命令过
};

//数据库命令值结构体
struct DB_Command_Value {
	char *stmtName;
	int nParams;
	char *paramValues[MAXPARAMNUM];
	int *paramLengths;
	int *paramFormats;
	int resultFormat;
};

//数据库实体类返回的结果
struct DB_Rsult_Data {
	void *data;
	int datalen;
};

struct DB_Result_func_Array {
	int length;
	char *sql_name[SQL_NUM];
	int(* func[SQL_NUM])(PGresult *result, DB_Rsult_Data *result_data);
};






//Imconfigure保存的结构体
struct IMConfigureSave{
	char *configure_name;
	char *configure_value;
	time_t lastQueryTime;
	IMConfigureSave *next;
};